package com.dddframework.mq.redis;

import com.dddframework.core.context.BaseContext;
import com.dddframework.core.contract.constant.ContextConstants;
import com.dddframework.core.utils.JsonKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 消息发布;
 * 目前只有 redis实现
 * 可以考虑MQ
 *
 * @author Liyj
 */
@Slf4j(topic = "### BASE-CORE : RedisMQPublisher ###")
public class RedisMQPublisher {
    public static <T> void publish(String key, T payload) {
        ObjectRecord<String, MQMessage<T>> record = StreamRecords.objectBacked(new MQMessage<>(payload)).withStreamKey(key);
        RedisTemplate redisTemplate = BaseContext.get(ContextConstants.REDIS_TEMPLATE);
        redisTemplate.opsForStream().add(record);
        log.debug("\n ---- 入队列：key:{};value:{}", key, JsonKit.toJsonClean(payload));
    }

}
